package com.hst.capacity.application.biz.impl;

import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.VehicleBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.driver.DriverCarEntity;
import com.hst.capacity.domain.model.entity.setting.price.ExpressStpPriceEntity;
import com.hst.capacity.domain.model.entity.vehicle.VehicleEntity;
import com.hst.capacity.domain.model.entity.vehicle.VehicleInsuranceEntity;
import com.hst.capacity.domain.model.entity.vehicle.VehicleLicenseEntity;
import com.hst.capacity.domain.model.entity.vehicle.VehicleTypeEntity;
import com.hst.capacity.domain.model.request.driver.DriverBandCarParam;
import com.hst.capacity.domain.model.request.vehicle.*;
import com.hst.capacity.domain.model.response.LabelValueVO;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.repository.redis.CacheRedis;
import com.hst.capacity.domain.service.DriverService;
import com.hst.capacity.domain.service.VehicleService;
import com.hst.capacity.infra.convertor.VehicleConvertor;
import com.hst.capacity.infra.persistence.jpa.driver.DriverRepository;
import com.hst.capacity.infra.persistence.jpa.setting.price.ExStpPriceRepository;
import com.hst.capacity.infra.persistence.jpa.vehicle.*;
import com.hst.capacity.infra.persistence.mybatis.VehicleMapper;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VehicleBizImpl implements VehicleBiz {

    @Autowired
    private VehicleTypeRepository vehicleTypeRepository;
    @Autowired
    private ExStpPriceRepository exStpPriceRepository;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private VehicleRepository vehicleRepository;
    @Autowired
    private DriverCarRepository driverCarRepository;
    @Autowired
    private DriverRepository driverRepository;
    @Autowired
    private DriverService driverService;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    VehicleInsuranceRepository insuranceRepository;
    @Autowired
    VehicleLicenseRepository licenseRepository;
    @Autowired
    CacheRedis cacheRedis;

    /**
     *获取地区下的所有车辆类型
     * @param param
     * @return
     */
    @Override
    public List<LabelValueVO> findVehicleTypeByStationInfo(VehicleTypeParam param) {

        List<ExpressStpPriceEntity> pLst = exStpPriceRepository.findByRegionCodeAndCarpoolType(param.getRegionCode(), Constants.OrderCarpoolType.Chartered);

        List<Integer> typeLst = new ArrayList<Integer>();
        for (int i = 0; i < pLst.size(); i++) {
            ExpressStpPriceEntity entity = pLst.get(i);
            //String idVehicleType = String.valueOf();
            if (!typeLst.contains(entity.getIdVehicleType()))
                typeLst.add(entity.getIdVehicleType());
        }

        return vehicleTypeRepository.findByIdVehicleTypes(typeLst);
    }

    /**
     * 添加/编辑车辆信息
     * @param param
     * @return
     */
    @Override
    public String addVehicle(VehicleAddParam param) {
        VehicleEntity vehicleEntity = vehicleService.addVehicle(param);
        //将车辆绑定司机
        if (StrUtil.isNotEmpty(param.getIdDriver()))
            driverService.driverBandVehicle(new DriverBandCarParam(param.getIdDriver(),vehicleEntity.getPlateNumber()));
        return vehicleEntity.getIdVehicle();
    }

    /**
     * 添加绑定关系
     * @param param
     * @return
     */
    @Override
    public List<DriverCarEntity> addVehicleRelation(VehicleRelationParam param) {
        VehicleEntity byPlateNumber = vehicleRepository.findByPlateNumber(param.getPlateNumber());
        if (driverRepository.findByIdDriver(param.getIdDriver())==null)
            throw new BusinessException(5002,"司机ID未找到请检查");
        if (byPlateNumber==null)
            throw new BusinessException(5002,"输入的车牌号不存在,请添加车辆");
        param.setIdVehicle(byPlateNumber.getIdVehicle());
        if (vehicleService.addDriverCar(param)==null)
            return null;
        List<DriverCarEntity> driverCarEntities = listVehicleRelation(param.getIdDriver());
        return driverCarEntities;
    }

    /**
     * 使用当前车辆
     * @param id
     * @return
     */
    @Override
    public Boolean activeVehicleRelation(String id){
        DriverCarEntity byIdDriverCar = driverCarRepository.findByIdDriverCar(id);
        if (byIdDriverCar.getActive()==1)
            throw new  BusinessException(5002,"当前车辆已被使用");
        byIdDriverCar.setActive(1);
        return driverCarRepository.save(byIdDriverCar)!=null;
    }
    /**
     * 删除绑定关系
     * @param id
     * @return
     */
    @Override
    public Boolean deleteVehicleRelation(String id){
        return driverCarRepository.deleteByIdDriverCar(id)==1;
    }

    /**
     * 获取司机下已绑定的车辆
     * @param id
     * @return
     */
    @Override
    public List<DriverCarEntity> listVehicleRelation(String id) {
        return driverCarRepository.findByIdDriver(id);
    }

    /**
     * 条件检索查询车辆
     * @param param
     * @return
     */
    @Override
    public PagingResultVO search(VehicleSearchParam param) {
        return new PagingResultVO(vehicleMapper.searchWithConditionsAndPagingTotal(param),vehicleMapper.searchWithConditionsAndPaging(param));
    }

    /**
     * 车辆添加保险
     * @param param
     * @return
     */
    @Override
    public VehicleInsuranceEntity addInsurance(VehicleAddInsuranceParam param) {
//        ArrayList<VehicleInsuranceEntity> returns = new ArrayList<>();
//        for (VehicleAddInsuranceParam v: param) {
//            returns.add(vehicleService.addInsurance(v));
//        }
        return vehicleService.addInsurance(param);
    }

    /**
     * 添加车辆资质
     * @param param
     * @return
     */
    @Override
    public VehicleLicenseEntity addCertificate(VehicleAddCertificateParam param) {
        if(vehicleRepository.findByIdVehicle(param.getIdVehicle())==null)
            throw new BusinessException(5002,"车辆不存在,请检查");
        return vehicleService.addCertificate(param);
    }

    /**
     * 车辆编辑查询
     * @param idVehicle
     * @return
     */
    @Override
    public Object vehicleEditSelect(String idVehicle,String moduleName) {
        Object re;
        if (moduleName.equals("vehicle")){
            re=vehicleService.vehicleEditSelectBase(idVehicle);
        }else if (moduleName.equals("insurance")){
            re=VehicleConvertor.INSTANCE.convert(insuranceRepository.findByIdVehicle(idVehicle));
        }else if (moduleName.equals("license")){
            re=VehicleConvertor.INSTANCE.convert(licenseRepository.findByIdVehicle(idVehicle));
        }else {
            throw new BusinessException(5002,"moduleName["+moduleName+"]不存在请检查");
        }
        return re;
    }

    @Override
    public Boolean vehicleDelete(String id) {
        return vehicleService.vehicleDelete(id);
    }

    /**
     *获取当前业务下所有车辆类型
     * @param param
     * @return
     */
    @Override
    public List<LabelValueVO> findVehicleType(VehicleTypeParam param) {

        List<LabelValueVO> ret = null;
        switch (param.getBizType()){
            //专车
            case Constants.BizType.SPECIAL:
                ret = vehicleTypeRepository.findSpVehicleType(param.getSearchKey());
                break;
            //城际
            case Constants.BizType.INTERCITY:
                ret = vehicleTypeRepository.findIcVehicleType(param.getSearchKey());
                break;
            //快车
            case Constants.BizType.EXPRESS:
                ret = vehicleTypeRepository.findExVehicleType(param.getSearchKey());
                break;
            //站点接驳
            case Constants.BizType.CARSTATION:
                ret = vehicleTypeRepository.findCarStationVehicleType(param.getSearchKey());
                break;
            //铁路接送
            case Constants.BizType.RAILSTATION:
                ret = vehicleTypeRepository.findRailStationVehicleType(param.getSearchKey());
                break;
            //接送机
            case Constants.BizType.AIRPORT:
                ret = vehicleTypeRepository.findAirportVehicleType(param.getSearchKey());
                break;
        }

        if(ret.size()==0){
            ret = vehicleTypeRepository.findDefVehicleType();
        }
        return ret;
    }


    /**
     *获取全部车辆类型
     * @return
     */
    @Override
    public List<LabelValueVO> findAllVehicleType() {
        return vehicleTypeRepository.findAllVehicleType();
    }

    @Override
    public String findVehicleType(int idVehicleType) {
        String name = cacheRedis.getVehicleTypeNameValue(String.valueOf(idVehicleType));
        if (StringUtil.isNotBlank(name)) {
            return name;
        } else {
            VehicleTypeEntity vehicleType = vehicleTypeRepository.findByIdVehicleType(idVehicleType);
            cacheRedis.writeVehicleTypeNameValue(String.valueOf(idVehicleType), vehicleType.getShortName());
            return vehicleType.getShortName();
        }
    }

}
